package cn.gdpu.course.web.controller.admin;

import cn.gdpu.course.constant.Constant;
import cn.gdpu.course.dao.TeacherDao;
import cn.gdpu.course.dao.domain.Teacher;
import cn.gdpu.course.web.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/4/1.
 */

@Controller
@RequestMapping("/admin/teacher")
public class AdmTeacherController {

	@Resource
	private TeacherDao teacherDao;

	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, ModelMap model) {
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		model.addAttribute("username", username);
		return "admin/teacher";
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public PageBean<Teacher> list(ModelMap model, Teacher teacher) {
		List<Teacher> teachers = teacherDao.find(teacher);
		PageBean<Teacher> data = new PageBean<Teacher>();
		data.setData(teachers);
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/findById", method = {RequestMethod.GET, RequestMethod.POST})
	public Teacher findById(ModelMap model, Teacher teacher) {
		return teacherDao.findById(teacher);
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
	public int save(ModelMap model, Teacher teacher) {
		Teacher tea = teacherDao.findByTno(teacher);
		if (tea != null) {
			return -1;
		}
		return teacherDao.save(teacher) ? 1 : 0;
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public Boolean update(ModelMap model, Teacher teacher) {
		return teacherDao.update(teacher);
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public Boolean delete(ModelMap model, Teacher teacher) {
		return teacherDao.delete(teacher);
	}

}
